﻿using PowerHub.MES.DTO.Shopfloor.QueryDTO;
using PowerHub.MES.EFBase;
using PowerHub.MES.Entities.Shopfloor;
using PowerHub.MES.Entities;
using PowerHub.MES.IRepository.IShopfloor;
using PowerHub.MES.ReturnEntity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PowerHub.MES.Entities.ProcessRoute;
using PowerHub.MES.IRepository.IProcessRoute;
using PowerHub.MES.DTO.ProcessRoute.QueryDto;
using Microsoft.EntityFrameworkCore;
using PowerHub.MES.Common.Utility;

namespace PowerHub.MES.Repository.ProcessRoute
{
    /// <summary>
    /// 工艺路线明细数据实现层
    /// </summary>
    public class ProcessRoute_CraftBillDetialRepository : BaseRepository<ProcessRoute_CraftBillDetial>, IProcessRoute_CraftBillDetialRepository
    {
        private PowerHubMesContext _repositorySys;
        public ProcessRoute_CraftBillDetialRepository(PowerHubMesContext repositorySys) : base(repositorySys)
        {
            _repositorySys = repositorySys;
        }


        /// <summary>
        /// 添加工艺路线明细
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> CreateProcessRoute_CraftBillDetialAsync(ProcessRoute_CraftBillDetial query)
        {
            var result = new OperationResult<bool>();
            if (AddEntity(query) > 0)
            {
                result.Data = true;
            }
            else
            {
                result.Data = false;

            }

            return result;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> DeletionProcessRoute_CraftBillDetialAsync(List<string>? ids)
        {
            var result = new OperationResult<bool>();

            var Departments = GetEntitys().AsNoTracking().Where(p => ids.Contains(p.Id)).ToList();
            Departments.ForEach(w => w.IsDel = 1);
            result.Data = UpdataEntityRange(Departments) > 0 ? true : false;

            return result;
        }

        /// <summary>
        /// 获取工艺路线明细列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationPageResult<List<ProcessRoute_CraftBillDetial>>> GetProcessRoute_CraftBillDetialPageListAsync(QueryProcessRoute_CraftBillDTO query)
        {
            var result = new OperationPageResult<List<ProcessRoute_CraftBillDetial>>();

            var currentQuery = GetEntitys().AsNoTracking()
                                            .Include(p=>p.Process)
                                            .Where(p => p.IsDel == 0)
                                            .WhereIf(!string.IsNullOrEmpty(query.CraftBillName), p => p.Process.ProcessName.Contains(query.CraftBillName))
                                            .Where(p=>p.CraftBillId.Equals(query.CraftBillId));

            result.PageIndex = query.PageIndex;
            result.PageSize = query.PageSize;
            result.TotalCount = await currentQuery.CountAsync();

            if (result.TotalCount > 0)
            {
                result.Data = await currentQuery.OrderBy(g => g.ProcessSort).Skip((query.PageIndex - 1) * query.PageSize).Take(query.PageSize).ToListAsync();
            }

            return result;
        }

        /// <summary>
        /// 更新工艺路线明细
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<OperationResult<bool>> UpdateProcessRoute_CraftBillDetialAsync(List<ProcessRoute_CraftBillDetial> query)
        {
            var result = new OperationResult<bool>();
            result.Data = false;
            if (UpdataEntityRange(query) > 0)
            {
                result.Data = true;
            }
            return result;
        }
    }
}
